package chain_token

import (
	"chain_bridge_service/configs"
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

func NewModel() *ChainToken {
	return new(ChainToken)
}

func (c *ChainToken) List(client *mongo.Client) ([]ChainToken, error) {

	cfg := configs.Get().Mongodb
	var chainTokenList []ChainToken
	applyClient := client.Database(cfg.Config.Name).Collection("chain_token")
	find, err := applyClient.Find(context.TODO(), bson.M{"chainConfigId": c.ChainConfigId})
	if err != nil {
		return nil, err
	}
	err = find.All(context.TODO(), &chainTokenList)
	if err != nil {
		return nil, err
	}

	return chainTokenList, nil
}

func (c *ChainToken) GetInfoById(client *mongo.Client) ChainToken {

	cfg := configs.Get().Mongodb
	var chainToken ChainToken
	applyClient := client.Database(cfg.Config.Name).Collection("chain_token")
	objId, _ := primitive.ObjectIDFromHex(c.Id)
	_ = applyClient.FindOne(context.TODO(), bson.M{"_id": objId, "chainConfigId": c.ChainConfigId}).Decode(&chainToken)
	return chainToken
}
